<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>InitPHP框架 PHP框架 - A PHP Framework - 无限极分类</title>
<link href="static/common.css" type="text/css" rel="stylesheet" />
<meta name="keywords" content="php框架，国产php框架, initphp框架，MVC，分层体系" />
<meta name="description" content="initphp框架是一款国产php框架。initphp框架主要基于MVC模式，具备代码清晰，操作简单，功能齐全，开发快速，高效安全等特点，是您选择php框架的首选。" />
</head>
<body>
<div class="header">
  <div class="header_nav">InitPHP (A PHP Framework) 用户手册</div>
  <div class="header_div">
    <div class="header_title">第6章 6.7.15 无限极分类</div>
    <div class="header_right"><a href="6_7_14.htm">上一页</a> <a href="6_7_16.htm">下一页</a> </div>
  </div>
</div>


<div class="title">
  <h3>接口描述：</h3>
</div>
<p>Tree无限分类类。属于InitPHP框架扩展类，需要通过$this->getLibrary()方法获取</p>

<div class="title">
  <h3>使用方法：</h3>
</div>
<pre id="php" class="php">
$tree = $this->getLibrary('tree'); 
</pre>
<div class="nav"></div>
<div class="title">
  <h3>实例：</h3>
</div>
<pre id="php" class="php">
$tree = $this->getLibrary('tree'); 
$arr = array(      
	1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目'),      
	2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目'),      
	3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目'),      
	4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目'),      
	5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目'),      
	6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目'),      
	7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目')     
 ); 
//$arr = 数组，$mid = 父级id，$selectid = 选中的id，$code = HTML代码， $prefix = 前缀，$selected = 选中的样式 
$tree->get_tree($arr, $mid = 0, $selectid = 5); 
//获取子级别 
$tree->get_child($arr, 0); 
//获取父级别 
$tree->get_parent($arr, 5);</pre>
<div class="title">
  <h3>具体类：</h3>
</div>
<pre id="php" class="php">
if (!defined('IS_INITPHP')) exit('Access Denied!');   
/*********************************************************************************
 * InitPHP 2.1 国产PHP开发框架  扩展类库-无限极分类
 *-------------------------------------------------------------------------------
 * 版权所有: CopyRight By initphp.com
 * 您可以自由使用该源码，但是在使用过程中，请保留作者信息。尊重他人劳动成果就是尊重自己
 *-------------------------------------------------------------------------------
 * $Author:zhuli
 * $Dtime:2011-10-09 
***********************************************************************************/
class treeInit {
	
	private $parentid = 'parentid';
	private $id       = 'id';
	private $name     = 'name';
	
	/**
	 * 无限级分类树-初始化配置
	 * @param  array $config array('parentid'=>'', 'id' => '', 'name' =>'name')
	 * @return string|array
	 */
	public function init($config = array()) {
		if (!is_array($config)) return false;
		$this->parentid = (isset($config['parentid'])) ? $config['parentid'] : $this->parentid;
		$this->id = (isset($config['id'])) ? $config['id'] : $this->id;
		$this->name = (isset($config['name'])) ? $config['name'] : $this->name;
		return true;
	}
	
	/**
	 * 无限级分类树-获取树
	 * @param  array $tree 树的数组
	 * @param  int   $mid  初始化树时候，代表ID下的所有子集
	 * @param  int   $selectid  选中的ID值
	 * @param  string  $code  代码
	 * @param  string  $prefix  前缀
	 * @param  string  $selected  选中
	 * @return string|array
	 */
	public function get_tree($tree, $mid = 0, $selectid = 5, $code = "<option value='\$id' \$selecteds>\$prefix\$name</option>", $prefix = '|-', $selected = 'selected') {
		if (!is_array($tree)) return '';
		$temp = array();
		$string = '';
		foreach ($tree as $k => $v) {
			if ($v[$this->parentid] == $mid) {
				$id = $v[$this->id];
				$name = $v[$this->name];
				$selecteds = ($id == $selectid) ? $selected : '';
				eval("\$temp_code = \"$code\";");//转化
				$string .=  $temp_code;
				$string .=  $this->get_tree($tree, $v[$this->id], $selectid, $code, '&nbsp;&nbsp;' . $prefix);
			}
		}
		return $string ;
	}
	
	/**
	 * 无限级分类树-获取子类
	 * @param  array $tree 树的数组
	 * @param  int   $id   父类ID
	 * @return string|array
	 */
	public function get_child($tree, $id) {
		if (!is_array($tree)) return array();
		$temp = array();
		foreach ($tree as $k => $v) {
			if ($v[$this->parentid] == $id) {
				$temp[] = $v;
			}
		}
		return $temp;
	}
	
	/**
	 * 无限级分类树-获取父类
	 * @param  array $tree 树的数组
	 * @param  int   $id   子类ID
	 * @return string|array
	 */
	public function get_parent($tree, $id) {
		if (!is_array($tree)) return array();
		$temp = array();
		foreach ($tree as $k => $v) {
			$temp[$v[$this->id]] = $v;
		}
		$parentid = $temp[$id][$this->parentid];
		return $temp[$parentid];
	}
	
}
</pre>

<div class="footer">版权所有：<a href="">http://initphp.com</a> InitPHP (A PHP Framework) By @Aliyun_zhuli</div>
<script src="static/jquery.js" type="text/javascript"></script>
<link href="static/prettify/prettify.css" rel="stylesheet" type="text/css">
<script src="static/prettify/prettify.js" type="text/javascript"></script>
<script type="text/javascript" src="static/comm.js"></script>
</body>
</html>
